// JavaScript Document
// Cart.js/ checkout.php, cart.php
function initPage()
{
	var ship = 0;
	var shipping_country = document.getElementById('shipping_country').value;
	//alert(shipping_country);
	if (shipping_country != '')
	{
		document.getElementById('radio_'+shipping_country).checked = 'checked';
	}

	var tax_pro = document.getElementById('tax_pro').value;
	//alert(tax_pro);
	if (tax_pro != '')
	{
		document.getElementById('radio_'+tax_pro).checked = 'checked';
	}

}


function doCheckout()
{
	var sub_total = document.getElementById('hidden_sub_total').value;
	if (sub_total == '')
	{
		alert('Sub Total cannot be empty.');
		return false;
	}
	//alert(sub_total);
	var shipping_total = document.getElementById('hidden_shipping_total').value;
	//alert(shipping_total);
	var tax_total = document.getElementById('hidden_tax_total').value;
	//alert(tax_total);
	var total_total = document.getElementById('hidden_total_total').value;
	if (total_total == '')
	{
		alert('Grand Total cannot be empty.');
		return false;
	}
	//alert(total_total);
	
	document.getElementById('paypal_form').submit();

}

function stripChar(pid)
{	
	var qty = document.getElementById('qty_'+pid).value;
	//alert(qty);
	var param = "checkout.php?action=update&cid="+ pid +"&pid="+ pid +"&qty="+qty;
	//alert(param);
	document.getElementById('update_'+pid).href = param;
}

function toHomePage()
{
	window.location = 'index.php';
}

function shipping_onchange(country)
{
	//alert(country);
	var shipping_fee = document.getElementById('shipping_fee');
	var ship = 0;
	var shipping_country = document.getElementById('shipping_country');
	var shipping_total = document.getElementById('shipping_total');
	var sub_total = document.getElementById('sub_total');
	var tax_total = document.getElementById('tax_total');
	var total_total = document.getElementById('total_total');
	var total_qtys = document.getElementById('total_quantities');
	var sf = 0;
	
	if (total_qtys.value <=10 && total_qtys.value >= 1)
	{
		sf = 5;
	}
	if (total_qtys.value <=20 && total_qtys.value >= 11)
	{
		sf = 10;
	}
	if (total_qtys.value <=50 && total_qtys.value >= 21)
	{
		sf = 15;
	}
	if (total_qtys.value <=100 && total_qtys >= 51)
	{
		sf = 25;
	}
	
	
	
	if (country == 'ca')
	{
		shipping_fee.value = sf;
		ship = 10;
		shipping_country.value = 'ca';
	}
	else if (country == 'us')
	{
		shipping_fee.value = sf;
		ship = 20;		
		shipping_country.value = 'us';
	}
	else if (country == 'in')
	{
		shipping_fee.value = sf;
		ship = 30;		
		shipping_country.value = 'in';		
	}
	else if (country == 'notapp')
	{
		shipping_fee.value = sf;
		ship = 0;		
		shipping_country.value = 'notapp';
	}
	
	//shipping_total.innerHTML = ship.toFixed(2);
	//var st = parseFloat(sub_total.innerHTML,10);
	//total_total.innerHTML = (st+ ship).toFixed(2);
	document.getElementById('shipping_tax_form').submit();

}

function tax_onchange(pro)
{
	//alert(province);
	var tax_fee = document.getElementById('tax_fee');
	var tax_pro = document.getElementById('tax_pro');
	var shipping_total = document.getElementById('shipping_total');
	var sub_total = document.getElementById('sub_total');
	var tax_total = document.getElementById('tax_total');
	var total_total = document.getElementById('total_total');
	var tax = 0;
	
	if (pro == 'on') // ontario
	{
		tax_fee.value = 0.13;
		tax = 0.13;
		tax_pro.value = 'on';
	}
	else if (pro == 'al') // Alberta
	{
		tax_fee.value = 0.05;
		tax = 0.05;
		tax_pro.value = 'al';
	}
	else if (pro == 'bc') //British Columbia 
	{
		tax_fee.value = 0.05;
		tax = 0.05;
		tax_pro.value = 'bc';
	}
	else if (pro == 'sa') //Saskatchewan 
	{
		tax_fee.value = 0.05;
		tax = 0.05;
		tax_pro.value = 'sa';
	}
	else if (pro == 'mb') // Manitoba 
	{
		tax_fee.value = 0.05;
		tax = 0.05;
		tax_pro.value = 'mb';
	}
	else if (pro == 'qc') //Quebec
	{
		tax_fee.value = 0.05;
		tax = 0.05;
		tax_pro.value = 'qc';
	}	
	else if (pro == 'ns') // Nova Scotia 
	{
		tax_fee.value = 0.05;
		tax = 0.05;
		tax_pro.value = 'ns';
	}
	else if (pro == 'nb') //New Brunswick 
	{
		tax_fee.value = 0.05;
		tax = 0.05;
		tax_pro.value = 'nb';
	}
	else if (pro == 'pei') //P.E.I. 
	{
		tax_fee.value = 0.05;
		tax = 0.05;
		tax_pro.value = 'pei';
	}
	else if (pro == 'nl') //Newfoundland 
	{
		tax_fee.value = 0.05;
		tax = 0.05;
		tax_pro.value = 'nl';
	}
	else if (pro == 'yu') // Yukon 
	{
		tax_fee.value = 0.05;
		tax = 0.05;
		tax_pro.value = 'yu';
	}
	else if (pro == 'nw') //North West Territories
	{
		tax_fee.value = 0.05;
		tax = 0.05;
		tax_pro.value = 'nw';
	}
	else if (pro == 'na') // Nunavut
	{
		tax_fee.value = 0.05;
		tax = 0.05;
		tax_pro.value = 'na';
	}
	else if (pro == 'oc') // Other Country 
	{
		tax_fee.value = 0.13;
		tax = 0.13;
		tax_pro.value = 'oc';
	}
	else if (pro == 'notapp') // not applicable
	{
		tax_fee.value = 0;
		tax = 0;
		tax_pro.value = 'notapp';
	}
	
	document.getElementById('shipping_tax_form').submit();
	//var st = parseFloat(sub_total.innerHTML,10);
	//var sf = parseFloat(shipping_total.innerHTML,10);
	//tax_total.innerHTML = ((st + sf ) * tax).toFixed(2);
	//total_total.innerHTML = (st + sf +(st + sf ) * tax).toFixed(2);
	
	
	
}